package com.dc.management.services;

import java.util.List;

import com.dc.management.dao.UserDAO;
import com.dc.management.pojo.TbModule;
import com.dc.management.pojo.TbUser;
import java.io.*;

/**
 * 
 */
public class UserService {

	private UserDAO dao;

	public UserService() {
		dao = new UserDAO();
	}

	/**
	 * 
	 */
	public TbUser isLogin(TbUser user) {
		return dao.isExiest(user);
	}
	
	
	public List getAllUser(){
		return dao.findAllUser();
	}

	/**
	 * generate the dtree.js ,which is menu 
	 */
	public void configureUserInfo(int user_id, String realPath) {
		List<TbModule> list = dao.findModuleInfoByUserId(user_id);
		StringBuffer sb = new StringBuffer();
		sb.append("d = new dTree('d'); \n");
		sb.append("d.add(0,-1,'系统菜单'); \n ");
		for (int i = 0; i < list.size(); i++) {
			TbModule module = (TbModule) list.get(i);
			sb.append("d.add(" + module.getModule_id() + ",");
			sb.append(module.getUpper_module_id() + ",'");
			sb.append(module.getModule_name());
			
			if( module.getUrl() != null ){
				sb.append(  "',\"javascript:navigate('");
				sb.append(module.getUrl());
				sb.append("')\");\n");
			}else{
				sb.append("');\n" );
			}
			
			
		}
		sb.append(" document.write(d);  \n");
		saveStringToFile(realPath, user_id + ".js", sb.toString());
		
	}

	
	public void saveStringToFile(String realPath, String filename,
			String content) {
		try{
			File file = new File(realPath);
			file.mkdirs();
			String path = realPath + filename;
			FileOutputStream fos = new FileOutputStream(path);
			BufferedOutputStream bos = new BufferedOutputStream(fos);
			bos.write(content.getBytes());
			bos.flush();
			bos.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

}
